<template>
	<view class="index" v-if="isLoad">
		<!-- #ifndef H5 -->
		<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" title="登录" color="#000"
			backgroundColor="">
		</uni-nav-bar>
		<!-- #endif -->
		<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
		<view class="index-box">
			<view class="flex-center">
				<image class="logo" :src="configInfo.app_logo" mode="aspectFill"></image>
			</view>
			<view class="f-paragraph flex-center pt-sm">{{configInfo.terminal_slogan}}</view>
			<view class="index-btn">
				<view class="btn flex-center" :style="{backgroundColor: primaryColor,boxShadow: `0px 3px 7px ${primaryColor}`}" 
				@tap="$util.goUrl({url:`/login/pages/account`})">
					<view class="flex-y-center">
						<view class="iconfont iconpingguoshouji_iphone_2x c-base"></view>
						<text class="f-title c-base pl-sm">手机号登录</text>
					</view>
				</view>
			</view>
			<view class="login-other flex-center">
				<view class="login-other-line"></view>
				<text class="f-mini-title pl-sm pr-sm">其他登录方式</text>
				<view class="login-other-line"></view>
			</view>
			<view class="flex-center pt-lg mt-sm">
				<view class="login-wechat rel flex-center" @tap.stop="toChooseLogin('weixin')">
					<view class="login-icon-bg abs"></view>
					<i class="iconfont icon-weixin rel" style="font-size: 25px;" :style="{color: primaryColor}"></i>
				</view>
				<view class="login-apple rel flex-center f-title c-black" @tap.stop="toChooseLogin('apple')" v-if="is_ios_login">
					<view class="login-icon-bg abs"></view>
					<i class="iconfont icon-apple rel" style="font-size: 25px;" :style="{color: '#000'}"></i>
				</view>
			</view>
		</view>
		<uni-popup ref="show_rule_item" type="center" :maskClick="false">
			<view class="common-popup-content fill-base pd-lg radius-34">
				<view class="title">温馨提示</view>
				<view class="f-desc c-title mt-lg">
					登录即表示您已详细阅读并同意<span @tap.stop="goDetail(1)" :style="{color:primaryColor}">
						《用户隐私协议》
					</span>与<span @tap.stop="goDetail(2)" :style="{color:primaryColor}">
						《个人信息保护指引》
					</span>
				</view>
				<view class="button">
					<view @tap.stop="$refs.show_rule_item.close()" class="item-child">取消</view>
					<view @tap.stop="goLogin" class="item-child c-base"
						:style="{background: primaryColor,color:'#fff'}">确定</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="show_apply" type="center" :maskClick="false">
			<view class="common-popup-content fill-base pd-lg radius-34 rel">
				<view class="title">温馨提示</view>
				<view class="f-title text-bold c-title mt-lg">
					暂未入驻
				</view>
				<view class="button">
					<view @tap.stop="toUrl(1)" class="item-child" :style="{background: primaryColor,color:'#fff'}">公司入驻
					</view>
					<view @tap.stop="toUrl(2)" class="item-child" :style="{background: primaryColor,color:'#fff'}">服务人员入驻
					</view>
				</view>
				<!-- <view class="button">
					<view @tap.stop="$refs.show_apply.close()" class="item-child">取消</view>
				</view> -->
				<view class="flex-center abs popup-close" @tap.stop="$refs.show_apply.close()">
					<i class="iconfont iconguanbi"></i>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	export default {
		data() {
			return{
				isCheck:false,
				lockTap: false,
				is_ios_login: false,
				isLoad: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			location: state => state.user.location,
			loginType: state => state.user.loginType,
		}),
		async onLoad() {
			this.updateUserItem({
				key: 'autograph',
				val: ''
			})
			this.updateUserItem({
				key: 'isAppLogin',
				val: false
			})
			this.updateUserItem({
				key: 'userInfo',
				val: {}
			})
			this.updateUserItem({
				key: 'coachInfo',
				val: {}
			})
			try {
				uni.clearStorageSync();
				console.log('清除缓存')
				// #ifdef APP-NVUE
					plus.cache.clear()
				// #endif
				
			} catch (e) {}
			let [syserr, sysinfo] = await uni.getSystemInfo()
			let {
				browserVersion = '',
					platform
			} = sysinfo
			this.is_ios_login = platform === 'ios' && browserVersion.split('.')[0] * 1 > 12
			if(!this.configInfo.id ){
				await this.getConfigInfo({is_coach: 0})
			}
			this.isLoad = true
			// this.$refs.show_apply.open()
		},
		methods:{
			...mapActions(['getConfigInfo','getUserInfo','getMineInfo']),
			...mapMutations(['updateConfigItem', 'updateUserItem']),
			toChooseLogin(type) {
				this.updateUserItem({
					key: 'loginType',
					val: type
				})
				this.$refs.show_rule_item.open()
			},
			goLogin(){
				if (this.lockTap) return
				this.lockTap = true
				this.$refs.show_rule_item.close()
				// #ifdef H5
				this.toGzhLogin()
				// #endif	
				// #ifdef APP-PLUS
				this.toAppLogin()
				// #endif
			},
			// APP登录
			async toAppLogin() {
				// this.toConfirmLoginApp()
				// return
				this.$util.showLoading()
				let {
					loginType: provider
				} = this
			
				try {
					let [providerErr, providerData] = await uni.getProvider({
						service: 'oauth',
					});
					if (providerErr) {
						this.$util.hideAll()
						this.$util.showToast({
							title: providerErr
						})
						this.lockTap = false
						return
					}
					let [loginErr, loginData] = await uni.login({
						provider
					});
					console.log(loginErr, loginData)
					if (loginErr) {
						this.$util.hideAll()
						this.$util.showToast({
							title: loginErr
						})
						this.lockTap = false
						return
					}
					let [infoErr, infoData] = await uni.getUserInfo({
						provider
					})
					if (infoErr) {
						this.$util.hideAll()
						this.$util.showToast({
							title: infoErr
						})
						this.lockTap = false
						return
					}
			
					let {
						userInfo = {}
					} = infoData
					let {
						openId = ''
					} = userInfo
					console.log(userInfo , provider , '=========> ')
					this.$util.hideAll()
					if (!openId) return
					this.$util.showLoading({
						title: "登录中..."
					})
					try {
						let param = {
							type: provider == 'weixin' ? 2 : 3,
							param: {
								data: userInfo
							},
							init: 1
						}
						this.toConfirmLogin(param)
					} catch (e) {
						this.$util.hideAll()
						this.lockTap = false
					}
				} catch (e) {
					this.$util.hideAll()
					this.$util.showToast({
						title: `没有获取到用户信息，请确认已登录`
					});
					this.lockTap = false
				}
			},
			async toConfirmLogin(login_params) {
				let {
					loginType
				} = this
			
				let {
					type,
					param,
					init = 0
				} = login_params
			
				let methodType = {
					1: 'webLogin',
					2: 'appLogin',
					3: 'iosLogin'
				}
				console.log(methodType[type] , '=========> methodType')
				let methodModel = methodType[type]
				let user_info = await this.$api.base[methodModel](param)
				console.log(user_info, '======> user_info')
				if (init == 1) {
					this.updateUserItem({
						key: 'appLogin',
						val: param.data
					})
				}
			
			
				let {
					autograph,
					data
				} = user_info
				console.log(user_info, '======> user_info1')
				this.$util.hideAll()
				this.lockTap = false
				
				this.updateUserItem({
					key: 'isShowLogin',
					val: false
				})
				this.updateUserItem({
					key: 'userInfo',
					val: data
				})
				this.updateUserItem({
					key: 'autograph',
					val: autograph
				})
				
				let {
					coach_status = 0,
					company_status = 0
				} = data
				if([2,3].includes(coach_status) || [2,3].includes(company_status)){
					this.$util.goUrl({url: '/pages/home'})
					return
				}
				if([1,4].includes(coach_status)){
					this.$util.goUrl({url: `/user/pages/service-apply-new`})
					return 
				}
				if([1,4].includes(company_status)){
					this.$util.goUrl({url: `/user/pages/company-apply`})
					return
				}
				if(coach_status == 0 || company_status == 0){
					this.$refs.show_apply.open()
					return
				}
				
				this.$util.goUrl({
					url: '/pages/home',
					openType: 'reLaunch'
				})
				
			},
			// app调试登录
			toConfirmLoginApp() {
				let user_info = {
					"data": {
						"id": 824,
						"uniacid": 666,
						"openid": "oDG4342lxj6wUH73W7pKLzTTvL6w",
						"nickName": "蕭萧",
						"avatarUrl": "https:\/\/massage.cncnconnect.com\/attachment\/image\/666\/23\/05\/a2f0efd10dfa4190aa125bc7ae522d93.jpg",
						"create_time": 1673263802,
						"status": 1,
						"cap_id": 0,
						"city": "",
						"country": "",
						"gender": 0,
						"language": "",
						"province": "",
						"balance": 92230.44,
						"phone": "18284514093",
						"session_key": "UsZEVHhv8vdYpJ+\/YuEjIA==",
						"pid": 824,
						"cash": 374.67,
						"unionid": "o3fbU0SmMbQLD4BKPyxcoD1KhAko",
						"app_openid": "oOxoV6NEt8Zl3P6lA7JWLH7NxMNc",
						"web_openid": "oFcA90U0Dy1YtuX91vppB5_LYgio",
						"wechat_openid": "oDG4342lxj6wUH73W7pKLzTTvL6w",
						"last_login_type": 0,
						"new_cash": "339.42",
						"lock": 178,
						"is_fx": 1,
						"ios_openid": "0",
						"push_id": "",
						"alipay_number": "18284514093",
						"alipay_name": "",
						"ip": "222.211.236.197"
					},
					"autograph": "f99e8e0daa9fb6d3575e1a32a4b27c57",
					coach_status: 2
				}
			
				let {
					autograph,
					coach_status,
					data
				} = user_info
				this.$util.hideAll()
				this.updateUserItem({
					key: 'isShowLogin',
					val: false
				})
				this.updateUserItem({
					key: 'userInfo',
					val: data
				})
				this.updateUserItem({
					key: 'autograph',
					val: autograph
				})
				let {
					user_force_login,
					short_code_status = 0,
					plugAuth = {}
				} = this.configInfo
			
				let {
					coachport = false
				} = plugAuth
			
				this.updateUserItem({
					key: 'userPageType',
					val: [2, 3].includes(coach_status) && !coachport ? 2 : 1
				})
			
				let {
					phone = ''
				} = data
			
				let {
					pageArr,
					loginPage
				} = this
			
				let isToPhone = user_force_login == 1 && short_code_status && !phone
				let url = isToPhone ? `/user/pages/phone` : loginPage || `/pages/service`
				let split_url = url.split('?')[0]
				let isReLaunch = isToPhone || pageArr.includes(split_url)
				let openType = isReLaunch ? `reLaunch` : `navigateBack`
				this.$util.goUrl({
					url: isReLaunch ? url : 1,
					openType
				})
				this.lockTap = false
			},
			//微信登录
			async wxLogin() {
				this.$refs.show_rule_item.close()
				try {
					let [providerErr, providerData] = await uni.getProvider({
						service: 'oauth',
					});
					let [loginErr, loginData] = await uni.login({
						provider: 'weixin'
					});
					let [infoErr, infoData] = await uni.getUserInfo({
						provider: 'weixin'
					})
					let {
						userInfo = {}
					} = infoData
					let {
						openId = ''
					} = userInfo
					if (!openId) return
					this.$util.showLoading({
						title: "登录中..."
					})
					try {
						this.toLogin(userInfo)
					} catch (e) {
						this.$util.hideAll()
					}
				} catch (e) {
					this.$util.showToast({
						title: '请先安装微信或升级版本'
					});
				}
			
			},
			async webLogin(){
				let pageUrl = window.location.href;
				let {
					gzh_appid: appid,
				} = siteInfo
				let redirect_uri = encodeURIComponent(pageUrl);
				let authUrl =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
				window.location.href = authUrl;
			},
			async toLogin(userInfo) {
				console.log(userInfo)
				let user_info = await this.$api.base.appLogin({
					data: userInfo
				})
				this.updateUserItem({
					key: 'appLogin',
					val: userInfo
				})
				let {
					autograph,
					data
				} = user_info
				this.$util.hideAll()
				if(data){
					this.updateUserItem({
						key: 'userInfo',
						val: data
					})
				}
				
				if(autograph){
					this.updateUserItem({
						key: 'autograph',
						val: autograph
					})
				}
				
				if (!this.configInfo.id) {
					await this.getConfigInfo()
				}
				
				let {
					phone = ''
				} = data
				if(phone){
					this.getMineInfo()
				}
				//phone = 1
				this.$util.goUrl({
					url: phone ? `/pages/service` : `/user/pages/phone`,
					openType: `reLaunch`
				})
			},
			toUrl(type){
				if(type == 1){
					this.$util.goUrl({url: `/user/pages/company-apply`})
				}else{
					this.$util.goUrl({url: `/user/pages/service-apply-new`})
				}
				this.$refs.show_apply.close()
			},
			goDetail(type) {
				let {
					siteroot
				} = siteInfo
				let href = siteroot.split('index.php')[0]
				let page = type == 1 ? 'protocol' : 'information'
				let url = `${href}${page}.html`
				this.$util.goUrl({
					url,
					openType: 'web'
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #fff;
	}
	.index-box{
		padding-top: 96rpx;
		image.logo{
			width: 176rpx;
			height: 176rpx;
		}
		.index-btn{
			padding: 280rpx 80rpx 0 80rpx ;
			.btn{
				height: 94rpx;
				border-radius: 94rpx;
			}
		}
		.box-text{
			letter-spacing: 1px;
			line-height: 32rpx;
			color: #6C727F;
		}
		.login-other {
			padding-top: 120rpx;
			color: #142C57;
		
			.login-other-line {
				width: 64rpx;
				border-bottom: 1px solid #677792;
			}
		}
		.login-wechat{
			width: 76rpx;
		}
		.login-wechat, .login-apple {
			height: 76rpx;
		
			.login-icon-bg {
				width: 100%;
				height: 76rpx;
				background: #44A860;
				opacity: 0.1;
				border-radius: 76rpx;
			}
		}
		.login-apple{
			width: 76rpx;
			margin-left: 50rpx;
		}
		
	}
	.popup-close{
		right: 30rpx;
		top: 30rpx;
	}
</style>